# -*- encoding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution
#    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

import time

from openerp.osv import fields, osv
from openerp.tools.translate import _

class delete_po_line(osv.osv_memory):
    _name = 'delete.po.line'
    _description = 'Delete PO Line'

    def default_get(self, cr, uid, fields, context=None):
#        raise osv.except_osv(_('Error !'), _('This button still in progress mode'))
        if context is None:
            context = {}
        purchase_order_line_obj = self.pool.get('purchase.order.line')
        stock_move_obj = self.pool.get('stock.move')
        product_uom_obj = self.pool.get('product.uom')
        res = super(delete_po_line, self).default_get(cr, uid, fields, context=context)
        for lines in purchase_order_line_obj.browse(cr, uid, context.get(('active_ids'), []), context=context):
            qty_delivery = 0.00
            move_ids = stock_move_obj.search(cr, uid, [('purchase_line_id','=',lines.id),('state','!=','cancel')])
            if move_ids:
                for mv in stock_move_obj.browse(cr, uid, move_ids, context=context):
                    qty_delivery = qty_delivery +  mv.product_uom_qty

            if 'qty_received' in fields:
                res.update({'qty_received': qty_delivery})

        return res


    def delete_line(self, cr, uid, ids, context=None):
        purchase_order_line_obj = self.pool.get('purchase.order.line')
        purchase_order_obj = self.pool.get('purchase.order')
        for obj in self.browse(cr, uid, ids, context=context):
            if obj.qty_received > 0:
                raise osv.except_osv(_('Error !'), _('cannot delete this line /n, because some qty has received from this line'))
            pol_val = purchase_order_line_obj.browse(cr, uid, context.get(('active_ids'), [])[0], context=context)
            po_id = pol_val.order_id.id
#             amount_untaxed = purchase_order_obj.browse(cr, uid, po_id, context=context).amount_untaxed
#             amount_tax = purchase_order_obj.browse(cr, uid, po_id, context=context).amount_tax
#             amount_total = purchase_order_obj.browse(cr, uid, po_id, context=context).amount_total
#              
#             line_untax =  pol_val.price_subtotal
#             #line_tax = purchase_order_obj._amount_line_tax(cr, uid, pol_val, context=context)
            purchase_order_line_obj.write(cr, uid, pol_val.id, {'state':'cancel'}, context=context)

            purchase_order_line_obj.unlink(cr, uid, context.get(('active_ids'), []))

            mod_obj = self.pool.get('ir.model.data')
            dummy, action_id = tuple(mod_obj.get_object_reference(cr, uid, 'purchase', 'purchase_form_action'))
            action = self.pool.get('ir.actions.act_window').read(cr, uid, action_id, context=context)
            res = mod_obj.get_object_reference(cr, uid, 'purchase', 'purchase_order_form')
            action['views'] = [(res and res[1] or False, 'form')]
            action['res_id'] = po_id or False
        return action

    _columns = {
        'qty_received': fields.float('Qty Receipt', readonly=True),
    }

delete_po_line()
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
